package com.xysq.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.umeng.analytics.MobclickAgent;
import com.xysq.adapter.ViewPagerAdapter;
import com.xysq.fragment.OutOfDateTicketsFragment;
import com.xysq.fragment.UnUseTicketsFragment;
import com.xysq.fragment.UseTicketsFragment;
import com.xysq.lemon.R;
import com.xysq.util.UserInfoKeeper;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;

/**
 * @Author huangdongkai 2015-7-24
 * 我的券包
 */
public class MyTicketsActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    @InjectView(R.id.group_tabs)
    RadioGroup tabsGroup;
    @InjectView(R.id.btn_unuse)
    RadioButton unUseBtn;
    @InjectView(R.id.btn_use)
    RadioButton useBtn;
    @InjectView(R.id.btn_out_of_date)
    RadioButton outOfDateBtn;
    @InjectView(R.id.view_pager)
    ViewPager pagerView;
    @InjectView(R.id.ibtn_back)
    ImageButton backIbtn;

    public static final int TYPE_UNUSE = 0;
    public static final int TYPE_USE = 1;
    public static final int TYPE_OUT_OF_DATE = 2;
    private UnUseTicketsFragment unUseTicketsFragment;
    private UseTicketsFragment useTicketsFragment;
    private OutOfDateTicketsFragment outOfDateTicketsFragment;
    private List<Fragment> fragmentList = new ArrayList<Fragment>();
    private ViewPagerAdapter viewPagerAdapter;
    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_ticket_bag);
        initView();
    }

    private void initView() {
        if (TextUtils.isEmpty(UserInfoKeeper.readPhoneNumber(this))) {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
        tabsGroup.setOnCheckedChangeListener(this);
        if (unUseTicketsFragment == null) {
            unUseTicketsFragment = UnUseTicketsFragment
                    .newInstance(TYPE_USE);
        }
        if (useTicketsFragment == null) {
            useTicketsFragment = UseTicketsFragment
                    .newInstance(TYPE_UNUSE);
        }
        if (outOfDateTicketsFragment == null) {
            outOfDateTicketsFragment = OutOfDateTicketsFragment
                    .newInstance(TYPE_OUT_OF_DATE);
        }
        fragmentList.add(unUseTicketsFragment);
        fragmentList.add(useTicketsFragment);
        fragmentList.add(outOfDateTicketsFragment);
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentList);
        pagerView.setAdapter(viewPagerAdapter);
        pagerView.setOffscreenPageLimit(fragmentList.size());
        pagerView.setCurrentItem(index);
        tabsGroup.check(R.id.btn_unuse);
        pagerView.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        unUseBtn.setChecked(true);
                        break;
                    case 1:
                        useBtn.setChecked(true);
                        break;
                    case 2:
                        outOfDateBtn.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.btn_unuse:
                pagerView.setCurrentItem(0);
                break;
            case R.id.btn_use:
                pagerView.setCurrentItem(1);
                break;
            case R.id.btn_out_of_date:
                pagerView.setCurrentItem(2);
                break;
        }
    }

    public void onBack(View view) {
        onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("我的券包页");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("我的券包页");
        MobclickAgent.onPause(this);
    }

    @Override
    public void finish() {
        Intent intent = new Intent(this,MainTabActivity.class);
        intent.putExtra(MainTabActivity.INDEX, MainTabActivity.TAG_USER);
        startActivity(intent);
    }
}
